<?php

namespace App\Http\Controllers\Home;

use App\Helpers\Content;
use App\Helpers\HTML;
use App\Model\Category;
use App\Model\FollowUser;
use App\Model\WeixinShare;
use App\Models\Archive\Archive;
use App\Models\Tag\Tag;
use Illuminate\Foundation\Auth\User;
use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Pagination\Paginator;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Input;



class contentController extends Controller
{
    public function contentLists(Request $request, $cate_id)
    {

        //获取栏目及其子栏目
        $cate2 = Category::where('cate_name','视频游戏')->first();
        if($cate2){
            $son = Category::where('cate_pid',$cate2->cate_id)->count();
            if($son){
                $cate_son = Category::where('cate_pid',$cate2->cate_id)->get();
                $cate_arr2 = [];
                foreach($cate_son as $v){
                    $cate_arr2[] = $v->cate_id;
                }
                array_push($cate_arr2,$cate2->cate_id);
                $cate_ids2 = implode(',',$cate_arr2);
                $video_archives =   Archive::where('archive_type_id',1)
                    ->whereIn('category_id',$cate_ids2)
                    ->orderBy('updated_at','desc')
                    ->take(6)
                    ->get()
                ;
            }else{
                $video_archives =   Archive::where('archive_type_id',1)
                    ->where('category_id',$cate2->cate_id)
                    ->orderBy('updated_at','desc')
                    ->take(6)
                    ->get();
            }
        }else{
            $video_archives = false;
        }

        //游戏攻略
        $cate3 = Category::where('cate_name','游戏攻略')->first();
        if($cate3){
            $son = Category::where('cate_pid',$cate3->cate_id)->count();
            if($son){
                $cate_son = Category::where('cate_pid',$cate3->cate_id)->get();
                $cate_arr3 = [];
                foreach($cate_son as $v){
                    $cate_arr3[] = $v->cate_id;
                }
                array_push($cate_arr3,$cate3->cate_id);
                $cate_ids3 = implode(',',$cate_arr3);
                $game_archives =   Archive::whereIn('category_id',$cate_ids3)
                    ->orderBy('updated_at','desc')
                    ->take(3)
                    ->get()
                ;
            }else{
                $game_archives =   Archive::where('category_id',$cate3->cate_id)
                    ->orderBy('updated_at','desc')
                    ->take(3)
                    ->get()
                ;
            }

        }else{
            $game_archives = false;
        }
        $cate = Category::where('cate_id',$cate_id)->first();
        $archives = Archive::ofPattern('review')
            ->where('category_id', $cate_id)
            ->orderBy('weight', 'desc')
            ->orderBy('created_at', 'desc')
            ->paginate(8);
        return view('pc_home.newsList')
            ->with('archives', $archives)
            ->with('cate',$cate)
            ->with('video_archives',$video_archives)
            ->with('game_archives',$game_archives)
        ;
    }

    public function detail(Request $request, Archive $archive)
    {
        $route = \Route::currentRouteName();
        if($archive->archive_type_id==1||$archive->archive_type_id==4){
                if(!($route=='article.show'||$route=='article.show_m')){
                    return response()->view('errors.code', ['msg' => '链接有误！', 'code' => 500], 500);
                }
        }elseif ($archive->archive_type_id==2){
            if(!($route=='gallery.show'||$route=='gallery.show_m')){
                return response()->view('errors.code', ['msg' => '链接有误！', 'code' => 500], 500);
            }
        }elseif ($archive->archive_type_id==3){
            if(!($route=='video.show'||$route=='video.show_m')){
                return response()->view('errors.code', ['msg' => '链接有误！', 'code' => 500], 500);
            }
        }

        $user = session('user');
        if($user){
            $login = 1;
        }else{
            $login = -1;
        }
        $review = $archive->hasPattern('review') ? true : false;
        $is_del = Archive::where('id',$archive->id)->value('is_del');
        if (!$review && (!$user || !($user->id == $archive->user_id || $user->master)))
            return response()->view('errors.code', ['msg' => '该内容尚未通过审核！', 'code' => 500], 500);

        if ($is_del)
            return response()->view('errors.code', ['msg' => '该内容已被删除！', 'code' => 500], 500);

        $archive->visit($request);
        if($user){
            $author_id = $archive->user->id;
            $follow = FollowUser::where('user_id',$user->id)->first();
            if($author_id == $follow['followed_id']){
                $followed = 1;
            }else{
                $followed = 0;
            }
        }else{
            $followed = -1;
        }

        $recommend = '';
        $similar = '';
        $new = '';
        $arc_count = '';
        $follow_count = '';
        $category = '';
        $cate_info = Category::where('cate_id',$archive->category_id)->first();

        if($archive->archive_type_id==1 || $archive->archive_type_id==4){
            $cate_id = Category::where('cate_name','好文')->value('cate_id');
            $category = Category::where('cate_pid',$cate_id)->where('is_on',0)->orderBy('cate_weight','asc')->orderBy('cate_id','desc')->get();
            //帖子数
            $arc_count = Archive::where('user_id',$archive->user_id)->count();
            //粉丝数
            $follow_count = FollowUser::where('followed_id',$archive->user_id)->count();

            if($archive->archive_type_id==1) {
                //最新文章
                $new = Archive::where('archive_type_id', 1)->where('user_id', $archive->user_id)->where('is_del', 0)->ofPattern('review')->orderBy('updated_at', 'desc')->take(3)->get();
                //热门点击
                $hot = Archive::where('archive_type_id', 1)->where('user_id', $archive->user_id)->where('is_del', 0)->ofPattern('review')->orderBy('visit_count', 'desc')->take(3)->get();
            }else{
                //最新文章
                $new = Archive::where('archive_type_id', 4)->where('is_del', 0)->ofPattern('review')->orderBy('updated_at', 'desc')->take(3)->get();
                //热门点击
                $hot = Archive::where('archive_type_id', 4)->where('is_del', 0)->ofPattern('review')->orderBy('visit_count', 'desc')->take(3)->get();

            }
            $agent = $_SERVER['HTTP_USER_AGENT'];
            if(strpos($agent,"NetFront") || strpos($agent,"iPhone") || strpos($agent,"MIDP-2.0") || strpos($agent,"Opera Mini") || strpos($agent,"UCWEB") || strpos($agent,"Android") || strpos($agent,"Windows CE") || strpos($agent,"SymbianOS")) {
                $i=1;
                while(!($left = Archive::where('id',$archive->id-$i)->whereIn('archive_type_id',[1,4])->where('is_del', 0)->ofPattern('review')->first())){
                    $i++;
                }
                $k=1;
                while(!($right = Archive::where('id',$archive->id+$k)->whereIn('archive_type_id',[1,4])->where('is_del', 0)->ofPattern('review')->first())){
                    $k++;
                }

                $share = new WeixinShare();
                $uri = $_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
                $share = $share->ticket($uri);

                $views = view('mobile.article.article_detail')->with('hot',$hot)->with('left',$left)->with('right',$right)->with('share',$share);
            }else{
                $views = view($archive->type->t_show)->with('hot',$hot);
            }
        }elseif($archive->archive_type_id==2){
            $cate_id = Category::where('cate_name','图库')->value('cate_id');
            $category = Category::where('cate_pid',$cate_id)->where('is_on',0)->orderBy('cate_weight','asc')->orderBy('cate_id','desc')->get();

            $images = $archive->detail->images()->get();
            $prev = Archive::where('archive_type_id',2)->where('is_del',0)->ofPattern('review')->where('id','<',$archive->id)->orderBy('updated_at','desc')->first();
            $next = Archive::where('archive_type_id',2)->where('is_del',0)->ofPattern('review')->where('id','>',$archive->id)->orderBy('updated_at','desc')->first();


            //推荐主播
            $new =  Tag::where('anchor_id', '<>',null)->where('is_rec',1)->orderBy('id','desc')->take(4)->get();

            $lastArea = Archive::where('archive_type_id',2)->where('is_del',0)->ofPattern('review')->orderBy('updated_at','desc')->take(6)->get();


            $agent = $_SERVER['HTTP_USER_AGENT'];
            if(strpos($agent,"NetFront") || strpos($agent,"iPhone") || strpos($agent,"MIDP-2.0") || strpos($agent,"Opera Mini") || strpos($agent,"UCWEB") || strpos($agent,"Android") || strpos($agent,"Windows CE") || strpos($agent,"SymbianOS")) {
                $views = view('mobile.gallery.gallery_detail')->with('lastArea',$lastArea)->with('images',$images)->with('next',$next)->with('prev',$prev);

            }else{
                $views = view($archive->type->t_show.$archive->view)->with('lastArea',$lastArea)->with('images',$images)->with('next',$next)->with('prev',$prev);
            }


        }elseif($archive->archive_type_id==3){
            $cate_id = Category::where('cate_name','视频')->value('cate_id');
            $category = Category::where('cate_pid',$cate_id)->where('is_on',0)->orderBy('cate_weight','asc')->orderBy('cate_id','desc')->get();
            //推荐视频
            $recommend = Archive::where('archive_type_id',3)->where('is_del',0)->ofPattern('recommend')->ofPattern('review')->orderBy('updated_at','desc')->take(3)->get();
            //最新视频
            $new = Archive::where('archive_type_id',3)->where('is_del',0)->ofPattern('review')->orderBy('updated_at','desc')->take(4)->get();
            //相关推荐
            $similar = $archive->tags()->get()->pluck('id');

            $similar = DB::table('archive_tag')->whereIn('tag_id', $similar)->take(6)->pluck('archive_id');

            $similar = Archive::whereIn('id', $similar)->where('is_del',0)->ofPattern('review')->orderBy('updated_at','desc')->get();

            $agent = $_SERVER['HTTP_USER_AGENT'];
            if(strpos($agent,"NetFront") || strpos($agent,"iPhone") || strpos($agent,"MIDP-2.0") || strpos($agent,"Opera Mini") || strpos($agent,"UCWEB") || strpos($agent,"Android") || strpos($agent,"Windows CE") || strpos($agent,"SymbianOS")) {

                $share = new WeixinShare();
                $uri = $_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
                $share = $share->ticket($uri);
                $views = view('mobile.video.video_detail')->with('share',$share);
            }else{
                $views = view($archive->type->t_show);
            }


        }
        $content = new Content($archive->detail->content);

        return $views
            ->with('archive', $archive)
            ->with('content', $content)
            ->with('followed',$followed)
            ->with('user',$user)
            ->with('arc_count',$arc_count)
            ->with('follow_count',$follow_count)
            ->with('new',$new)
            ->with('review', $review)
            ->with('category', $category)
            ->with('recommend', $recommend)
            ->with('similar', $similar)
            ->with('cate_info', $cate_info)
            ->with('login', $login)
        ;
    }

    public function like(Request $request, Archive $archive)
    {
        $user = session('user');

        if (!$user) redirect('/auth');

        if ($archive->isLiked($user)) {
            return response()->json(['msg' => '您已经收藏过该文章了！', 'code' => 1]);
        } else {
            $archive->like($user);
            return response()->json(['msg' => '收藏成功！', 'code' => 0]);
        }
    }

    public function changeFollow()
    {
        $user = session('user');
        if($input = Input::except('_token')){
            if($input['followed']){
                if(FollowUser::where('user_id',$user->id)->where('followed_id',$input['followed_id'])->delete()){
                    return response()->json(['rs'=>'success','msg'=>'取消关注成功！']);
                }else{
                    return response()->json(['rs'=>'error','msg'=>'取消关注失败！']);
                }
            }else{
                $data['user_id'] = $user->id;
                $data['followed_id'] = $input['followed_id'];
                $data['followed_at'] = date('Y-m-d H:i:s',time());
                if(FollowUser::where('user_id',$user->id)->where('followed_id',$input['followed_id'])->first()){
                    return response()->json(['rs'=>'success','msg'=>'您已关注过！']);
                }else if(FollowUser::create($data)){
                    return response()->json(['rs'=>'success','msg'=>'关注成功！']);
                }else{
                    return response()->json(['rs'=>'error','msg'=>'关注失败！']);
                }
            }
        }else{
            return response()->json(['rs'=>'error','msg'=>'非法请求！']);
        }
    }

    public function ajaxGetImages(Archive $archive)
    {
        $images = $archive->detail->images()->get();
        return response()->json($images);
    }
}
